package com.depth.viewer.container
{
	import com.depth.viewer.events.AppEvent;
	import com.depth.viewer.events.EventBus;
	import com.depth.viewer.widget.IBaseWidget;
	
	import flash.events.Event;
	import flash.external.ExternalInterface;
	
	import mx.controls.Alert;
	import mx.core.FlexGlobals;
	import mx.core.IVisualElement;
	import mx.events.ModuleEvent;
	import mx.logging.Log;
	import mx.logging.LogEventLevel;
	import mx.logging.targets.TraceTarget;
	import mx.managers.PopUpManager;
	import mx.modules.IModuleInfo;
	import mx.modules.ModuleManager;
	
	import spark.components.Group;
	import spark.layouts.VerticalLayout;

	public final class ViewerContainer extends Group
	{
		public function ViewerContainer()
		{
			super();
			this.percentHeight = 100;
			this.percentWidth  = 100;
			
		}
		public static const CONTAINER_INITIALIZED:String = "containerInitilized";
		
		public var widgetContainer:BaseLayoutContainer;
		
		private static var _container:ViewerContainer;
		
		private static var _lock:Boolean = false;
		
		
		private function init():void
		{
			_container = this;
			_lock = true; 
			
			ViewerContainer.addEventListener(AppEvent.APP_ERROR, errorHandler);
			
			ViewerContainer.dispatch(ViewerContainer.CONTAINER_INITIALIZED);
		}
		
		private var isAddChild:Boolean = true;
		
		protected override function commitProperties():void
		{
			super.commitProperties();
			
			if (isAddChild && widgetContainer)
			{
				addElement(widgetContainer);
				isAddChild = false;
			}
		}
		
	
		private function errorHandler(event:AppEvent):void
		{
			
		}
		
	
		public static function getInstance():ViewerContainer
		{
			if (!_lock)
			{
				_container = new ViewerContainer();
				_lock = true;
			}
			return _container;
		}
		
		//the following are the methods to allow modules access event bus via the contains.
		public static function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
		{
			EventBus.getInstance().addEventListener(type, listener, useCapture, priority, useWeakReference);
		}
		
		public static function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void
		{
			EventBus.getInstance().removeEventListener(type, listener, useCapture);
		}
		
		//this is the simplest way of communicate. Just use a string.
		public static function dispatch(message:String):Boolean
		{
			return EventBus.getInstance().dispatch(message);
		}
		
		public static function dispatchEvent(event:Event):Boolean
		{
			return EventBus.getInstance().dispatchEvent(event);
		}
		
		public static function showError(value:String):void
		{
			dispatchEvent(new AppEvent(AppEvent.APP_ERROR, value));
		}
	}
}